#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
ll sub[1000010], fir[1000100];
void solve()
{
    memset(sub,0,sizeof(sub));
    memset(fir, 0, sizeof(fir));
    ll n;
    cin >> n;
    map<ll, ll> mp;
    vector<ll> a(n + 1);
    for (ll i = 1; i <= n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }

    for (ll i = n; i >= 1; i--)
    {
        if (a[i] == 0)
        {
            sub[i] = sub[i + 1];
        }
        else
        {
            sub[i] = sub[i + 1] + (fir[a[i]]==0 ? 1 : 0);
        }
        fir[a[i]] = i;
    }

    // for(ll i= 1;i<=n;i++)
    // {
    //     cout << sub[a[i]] << " ";
    // }
    // cout << endl;
    ll ans = 0;
    ll last_0 = -1;
    for (ll i = 1; i <= n; i++)
    {
        if (a[i] == 0)
        {
            last_0 = i;
            continue;
        }
        else
        {
            if (last_0 > fir[a[i]])
            {
                ans += sub[i + 1];
                fir[a[i]] = 1e8;
            }
        }
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
